import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

const types = {
  SET_AUTHENTICATED: 'SET_AUTHENTICATED',
  SET_USER: 'SET_USER',
  SET_LOADING: 'SET_LOADING',
  SET_ROUTES: 'SET_ROUTES',
  SET_ISNAV:'SET_ISNAV'
}

const state = {
  isAuthenticated: false,
  user: {},
  isLoading: true,
  isNav: localStorage.getItem('isDing')?localStorage.getItem('isDing'):false
}

const getters = {
  isAuthenticated: state => state.isAuthenticated,
  user: state => state.user,
  isLoading: state => state.isLoading,
  routes: state => state.routes,
  isNav:state => state.isNav,
}

const mutations = {
  [types.SET_AUTHENTICATED](state, isAuthenticated) {
    if (isAuthenticated) state.isAuthenticated = isAuthenticated
    else state.isAuthenticated = false
  },
  [types.SET_USER](state, user) {
    state.user = user
  },
  [types.SET_ROUTES](state, routes) {
    state.routes = routes
  },
  [types.SET_ISNAV](state, isNav) {
    state.isNav = isNav
  },
  showLoading(state) {
    state.isLoading = true
  },
  hideLoading(state) {
    state.isLoading = false
  }
}

const actions = {
  setNav:({commit},isNav)=>{
    commit(types.SET_ISNAV, isNav)
  },
  setAuthenticated: ({
    commit
  }, isAuthenticated) => {
    commit(types.SET_AUTHENTICATED, isAuthenticated)
  },
  setUser: ({
    commit
  }, user) => {
    commit(types.SET_USER, user)
  },
  setRoutes: ({
    commit
  }, routes) => {
    commit(types.SET_ROUTES, routes)
  },
  clearCurrentState: ({
    commit
  }) => {
    commit(types.SET_AUTHENTICATED, false)
    commit(types.SET_USER, null)
  }
}

export default new Vuex.Store({
  state,
  getters,
  mutations,
  actions,
})
